//
//  PedometerViewController.m
//  LOU
//
//  Created by apple on 15/11/18.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "PedometerViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface PedometerViewController ()

@property (nonatomic, strong)CMPedometer *pedometer;
@property (nonatomic, strong)CMMotionActivityManager *manager;
@property (nonatomic, strong)NSOperationQueue *operationQueue;
@property (nonatomic, strong)UILabel *stepsLabel;
@property (nonatomic, strong)UILabel *statusLabel;
@property (nonatomic, strong)UILabel *confidenceLabel;




@end

@implementation PedometerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 100, 40)];
    self.statusLabel.backgroundColor = [UIColor yellowColor];
    self.statusLabel.textColor = [UIColor cyanColor];
    [self.view addSubview:self.statusLabel];
    
    
    self.stepsLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 100, 40)];
    self.stepsLabel.backgroundColor = [UIColor yellowColor];
    self.stepsLabel.textColor = [UIColor cyanColor];
    [self.view addSubview:self.stepsLabel];
    
        self.confidenceLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 300, 100, 40)];
        self.confidenceLabel.backgroundColor = [UIColor yellowColor];
        self.confidenceLabel.textColor = [UIColor cyanColor];
        [self.view addSubview:self.confidenceLabel];
    
    if (!([CMPedometer isStepCountingAvailable] || [CMMotionActivityManager isActivityAvailable])) {
        
        NSString *msg = @"哎喲，不能運行哦,這demo僅支持M7處理器, 所以暫時只能在iPhone5s上玩哦.";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps!!!"
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        
        return;
    }
    __weak PedometerViewController *weakSelf = self;
    
    self.operationQueue = [[NSOperationQueue alloc] init];
    
    //更新label
    if ([CMPedometer isStepCountingAvailable]) {
        
        self.pedometer = [[CMPedometer alloc] init];
        
        [self.pedometer startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData *pedometerData, NSError * error) {
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (error) {
                    UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Opps!" message:@"error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [error show];
                }
                else {
                    
                    NSString *text = [NSString stringWithFormat:@"步數: %@", pedometerData.numberOfSteps];
                    
                    weakSelf.stepsLabel.text = text;
                }
            }
                           );
            
        }];
        
        
    }
    
    
    if ([CMMotionActivityManager isActivityAvailable]) {
        
        self.manager = [[CMMotionActivityManager alloc] init];
        
        [self.manager startActivityUpdatesToQueue:self.operationQueue
                                              withHandler:
         ^(CMMotionActivity *activity) {
             
             dispatch_async(dispatch_get_main_queue(), ^{
                 
                 NSString *status = [weakSelf statusForActivity:activity];
                 NSString *confidence = [weakSelf stringFromConfidence:activity.confidence];
                 
                 weakSelf.statusLabel.text = [NSString stringWithFormat:@"狀態: %@", status];
                 weakSelf.confidenceLabel.text = [NSString stringWithFormat:@"速度: %@", confidence];
             });
         }];
    }

}
- (NSString *)statusForActivity:(CMMotionActivity *)activity {
    
    NSMutableString *status = @"".mutableCopy;
    
    if (activity.stationary) {
        
        [status appendString:@"not moving"];
    }
    
    if (activity.walking) {
        
        if (status.length) [status appendString:@", "];
        
        [status appendString:@"on a walking person"];
    }
    
    if (activity.running) {
        
        if (status.length) [status appendString:@", "];
        
        [status appendString:@"on a running person"];
    }
    
    if (activity.automotive) {
        
        if (status.length) [status appendString:@", "];
        
        [status appendString:@"in a vehicle"];
    }
    
    if (activity.unknown || !status.length) {
        
        [status appendString:@"unknown"];
    }
    
    return status;
}

- (NSString *)stringFromConfidence:(CMMotionActivityConfidence)confidence {
    
    switch (confidence) {
            
        case CMMotionActivityConfidenceLow:
            
            return @"Low";
            
        case CMMotionActivityConfidenceMedium:
            
            return @"Medium";
            
        case CMMotionActivityConfidenceHigh:
            
            return @"High";
            
        default:
            
            return nil;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
